
package com.chat.api.utils.util;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

public class DateUtil extends DateUtils {

	private static String[] parsePatterns = { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", "yyyy/MM/dd",
			"yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM", "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM" };

	private static DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
	private static DateFormat df_yyyy_mm_yy_hh_mm_ss = new SimpleDateFormat("yyyyMMddHHmmss");
	private static DateFormat df_yyyyMMdd = new SimpleDateFormat("yyyy-MM-dd");
	private static DateFormat df_yyyyMMdd1 = new SimpleDateFormat("yyyy/MM/dd");
	private static DateFormat df_yyyyMMdd2 = new SimpleDateFormat("yyyyMMdd");
	private static DateFormat df_yyyy_MM_ddT = new SimpleDateFormat("yyyy/MM/dd");

	public static final long SECOND = 1000;

	public static final long MINUTE = 60 * SECOND;

	public static final long HOUR = 60 * MINUTE;

	public static final long DAY = 24 * HOUR;

	public static final long WEEK = 7 * DAY;

	public static final long MONTH = 30 * DAY;

	public static final long YEAR = 365 * DAY;

	private static final SimpleDateFormat timeGapFormatter = new SimpleDateFormat("yyyyMMddHHmmss");

	/**
	 * 取得当前时间与给定时间的时间间隔，并按照特定格式返回
	  * @author zhouyu
	  * Date:Aug 1, 2007
	  * @param time 指定时间 格式：yyyyMMddHHmmss
	  * @return
	  * Return_Type:String 若无法取得结果，返回NULL
	  * Description:
	 */
	public static String getTimeGap(String time) {

		if (time.trim().length() != "yyyyMMddHHmmss".length()) {
			return null;
		}

		Date date;
		try {
			date = timeGapFormatter.parse(time);
		} catch (ParseException e) {
			return null;
		}

		long currentTime = System.currentTimeMillis();
		long timeGap = currentTime - date.getTime();

		if (timeGap / YEAR > 0) {
			return timeGap / YEAR + "年前";
		}
		if (timeGap / MONTH > 0) {
			return timeGap / MONTH + "月前";
		}
		if (timeGap / WEEK > 0) {
			return timeGap / WEEK + "周前";
		}
		if (timeGap / DAY > 0) {
			return timeGap / DAY + "天前";
		}
		if (timeGap / HOUR > 0) {
			return timeGap / HOUR + "小时前";
		}
		if (timeGap / MINUTE > 0) {
			return timeGap / MINUTE + "分钟前";
		}
		if (timeGap / SECOND > 0) {
			return timeGap / SECOND + "秒前";
		}

		return "1秒前";

	}

	private static final String getDateFormat(int flag) {
		String tempStr = null;
		if (flag == 1)
			tempStr = "yyyyMMdd";
		else if (flag == 2)
			tempStr = "yyyyMMddHHmmss";
		else if (flag == 3)
			tempStr = "HHmmss";
		else if (flag == 4)
			tempStr = "HH:mm";
		else if (flag == 5)
			tempStr = "HH:mm:ss";
		else if (flag == 6)
			tempStr = "yyyy-MM-dd";
		else if (flag == 7)
			tempStr = "yyyy-MM-dd(E)";
		else if (flag == 8)
			tempStr = "yyyy-MM-dd HH:mm:ss";
		else if (flag == 9)
			tempStr = "yyyy-MM-dd hh-mm-ss";
		else if (flag == 10)
			tempStr = "yyyy-MM-dd";
		else if (flag == 11)
			tempStr = "yyyy-MM-dd (HH)";
		else if (flag == 12)
			tempStr = "yyyy-MM-dd HH:mm";
		else if (flag == 13)
			tempStr = "yyyy年MM月dd日 E";
		else if (flag == 14)
			tempStr = "yyyy年M月dd日 ";
		else if (flag == 15)
			tempStr = "M月dd日 ";
		else if (flag == 16)
			tempStr = "yyyy年M月dd日  HH:mm:ss";
		else if (flag == 17)
			tempStr = "yyyy-MM-dd HH:mm:ss";
		else if (flag == 18)
			tempStr = "yyyy-MM-dd ??:??";
		else if (flag == 19)
			tempStr = "M月dd日 HH:mm";
		else if (flag == 20)
			tempStr = "yyyyMM";
		else if (flag == 21)
			tempStr = "yyyy_MM";
		else if (flag == 22)
			tempStr = "yyyy/MM/dd";
		else if (flag == 23)
			tempStr = "yyyy/MM/dd HH:mm:ss";
		else if (flag == 24)
			tempStr = "yyyy/MM/dd HH:mm";
		else if (flag == 25)
			tempStr = "yyyy-MM";
		else if (flag == 26)
			tempStr = "yyyy年M月dd日HH时mm分ss秒";
		return tempStr;
	}

	public static final String formatStrTime(String time, int srcf, int destf) {
		time = StringUtils.trimToNull(time);
		if (time == null) {
			return "";
		}
		try {
			Date d = strDateToSystem(time, srcf);
			return systemToStrDate(d, destf);
		} catch (Exception e) {
			return time;
		}
	}

	public static final String formatChinaStrTime(String time, int srcf, int destf) {
		time = StringUtils.trimToNull(time);
		if (time == null) {
			return "";
		}
		Date d = strChinaDateToSystem(time, srcf);
		return systemToStrDate(d, destf);
	}

	/**
	   * 2016/05/02_am_06 转换为系统时间
	   * @param strDate
	   * @return
	   */
	public static final Date strAmPmDateToSystem(String strDate) {
		String time = strDate.replaceAll("/", "-");
		String[] myd = time.split("_");
		String HH = "";
		if (myd[1].equals("pm") || myd[1].equals("pm")) {
			HH = String.valueOf(Integer.parseInt(myd[2].replaceAll("0", "")) + 12);
		} else {
			HH = myd[2];
		}
		StringBuilder sb = new StringBuilder();
		String MYDHH = sb.append(myd[0]).append(" ").append("(").append(HH).append(")").toString();
		return strDateToSystem(MYDHH, 11);
	}

	public static final Date strDateToSystem(String strDate) {
		return strDateToSystem(strDate, 2);
	}

	public static final Date strDateToSystem(String strDate, int flag) {

		strDate = StringUtils.trimToEmpty(strDate);

		String tempStr = getDateFormat(flag);
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(tempStr);
		if (strDate != null && !"".equalsIgnoreCase(strDate)) {
			try {
				return simpleDateFormat.parse(strDate);
			} catch (ParseException e) {
				return new Date();
			}
		} else
			return new Date();
	}

	public static final Date strToDate(String strDate, int flag) {

		strDate = StringUtils.trimToEmpty(strDate);

		String tempStr = getDateFormat(flag);
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(tempStr);
		if (strDate != null && !"".equalsIgnoreCase(strDate)) {
			try {
				return simpleDateFormat.parse(strDate);
			} catch (ParseException e) {
				e.printStackTrace();
				return null;
			}
		} else
			return null;
	}

	public static final Date msecToDate(long msec) {

		if (msec > 0) {
			try {
				Calendar c = Calendar.getInstance();
				c.setTimeInMillis(msec);
				Date date = c.getTime();
				return date;
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		} else
			return null;
	}

	public static final String msecToStrDate(long msec, int flag) {

		if (msec > 0) {
			Date date = msecToDate(msec);
			return dateToStr(date, flag);
		} else
			return "";
	}

	public static final Date strChinaDateToSystem(String strDate, int flag) {

		strDate = StringUtils.trimToEmpty(strDate);

		// SimpleDateFormat simpleDateFormat = new SimpleDateFormat(tempStr);
		DateFormat simpleDateFormat = DateFormat.getDateInstance(DateFormat.LONG, Locale.CHINESE);
		if (strDate != null && !"".equalsIgnoreCase(strDate)) {
			try {
				return simpleDateFormat.parse(strDate);
			} catch (ParseException e) {
				e.printStackTrace();
				return new Date();
			}
		} else
			return new Date();
	}

	/**
	 * 将日期格式化输出为String---yyyy/MM/dd HH:mm:ss
	 *
	 * @param date
	 * @return
	 */
	public static String format(Date date) {
		if (date == null)
			return "";
		return df.format(date);
	}

	public static final String formatDefault(Date date) {
		return df_yyyy_mm_yy_hh_mm_ss.format(date);
	}

	public static Date parseTimeDefault(String str) throws ParseException {
		return df_yyyy_mm_yy_hh_mm_ss.parse(str);
	}

	/**
	 * 将日期格式化输出为String
	 *
	 * @param date
	 * @return
	 */
	public static String formatYMD(Date date) {
		return df_yyyyMMdd.format(date);
	}

	public static String formatYMD(String longdate) {
		try {
			Date date = new Date(Long.parseLong(longdate));
			return df_yyyyMMdd.format(date);
		} catch (Exception e) {
			return "";
		}
	}

	public static String formatYMD2(String longdate) {
		try {
			Date date = new Date(Long.parseLong(longdate));
			return df_yyyyMMdd2.format(date);
		} catch (Exception e) {
			return "";
		}
	}

	/**
	 * 将日期格式化输出为String
	 *
	 * @param date
	 * @return
	 */
	public static String formatYMDDate(Date date) {
		return df_yyyyMMdd1.format(date);
	}

	/**
	 * 根据出生日期获得年龄
	 * @param birthday
	 * @return
	 */
	public static int getAge(Long birthday) {
		int age = 0;
		GregorianCalendar gc = new GregorianCalendar();
		GregorianCalendar now = (GregorianCalendar) gc.clone();
		gc.setTimeInMillis(birthday.longValue());
		age = now.get(Calendar.YEAR) - gc.get(Calendar.YEAR);
		if (age < 0) {
			age = 0;
		}
		return age;
	}

	/**
	 * 返回毫秒
	 *
	 * @param date
	 *            日期
	 * @return 返回毫秒
	 */
	public static long getMillis(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.getTimeInMillis();
	}

	/**
	 * 日期相加
	 *
	 * @param date
	 *            日期
	 * @param day
	 *            天数
	 * @return 返回相加后的日期
	 */
	public static Date addDate(Date date, int day) {
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(getMillis(date) + ((long) day) * 24 * 3600 * 1000);
		return c.getTime();
	}

	/**
	 * @param date
	 * @param minute
	 * @return 当前时间增加分钟
	 */
	public static Date addMinute(Date date, int minute) {
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(getMillis(date) + ((long) minute) * 60 * 1000);
		return c.getTime();
	}

	public static Date addSecond(Date date, long sec) {
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(getMillis(date) + (sec) * 1000);
		return c.getTime();
	}

	public static String getDateTime() {
		DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar objCalendar = Calendar.getInstance();
		String strReturn = formatter.format(objCalendar.getTime());
		return strReturn;
	}

	public static long formatSecond(String time) {
		String[] times = time.split(":");
		return Long.parseLong(times[0]) * 3600 + Long.parseLong(times[1]) * 60 + Long.parseLong(times[2]);
	}

	/**
	 *
	 * @param date1
	 * @param date2
	 * @return 返回两个时间相差的天数
	 */
	public static long daySubtract(String date1, String date2) {
		SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
		Date date;
		Date mydate;
		long day = 0;
		try {
			date = myFormatter.parse(date1);
			mydate = myFormatter.parse(date2);
			day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
		} catch (ParseException e) {
			e.printStackTrace();
			day = 0;
		}
		return day;

	}

	public static int daySubtract(Date date1, Date date2) {
		int day = 0;
		try {
			day = Math.round((date1.getTime() - date2.getTime()) / (24 * 60 * 60 * 1000));
		} catch (Exception e) {
			e.printStackTrace();
			day = 0;
		}
		return day;

	}

	/**
	*
	* @param date
	* @return 返回系统时间与指定日期相差的天数
	*/
	public static long daySubtract(String date) {
		String date1 = formatYMD(new Date());
		return daySubtract(date1, date);
	}

	/**
	* @author zhaojinpo
	* @date 2014-1-20 下午09:46:53
	* @param date1
	* @param date2
	* @return 返回两个时间相关的分钟数
	*/

	public static long minuteSubtract(String date1, String date2) {
		SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date date;
		Date mydate;
		long minute = 0;
		try {
			date = myFormatter.parse(date1);
			mydate = myFormatter.parse(date2);
			minute = (date.getTime() - mydate.getTime()) / (1 * 60 * 1000);
		} catch (ParseException e) {
			e.printStackTrace();
			minute = 0;
		}
		return minute;
	}

	public static long minuteSubtract(String date) {
		Date d = new Date();
		String nowDate = systemToStrDate(d, 12);
		return daySubtract(nowDate, date);
	}

	/**
	 * 将字符串格式化为日期
	 *
	 * @param str	格式为 yyyy/MM/dd HH:mm:ss
	 * @return
	 */
	public static Date parseTime(String str) throws ParseException {
		return df.parse(str);
	}

	public static String formatY_M_D(Date date) {
		return df_yyyy_MM_ddT.format(date);
	}

	public static String formatY_M_D(long dateLong) {
		Date date = new Date(dateLong);
		return df_yyyy_MM_ddT.format(date);
	}

	public static String formatDF(long dateLong) {
		Date date = new Date(dateLong);
		return df.format(date);
	}

	@SuppressWarnings("deprecation")
	public static int getYear(long dateLong) {
		Date date = new Date(dateLong);
		return date.getYear();
	}

	public static int getYearNow() {
		Calendar cal = Calendar.getInstance();
		int iYearNow = cal.get(1);
		return iYearNow;
	}

	public static int getMonthNow() {
		Calendar cal = Calendar.getInstance();
		int iMonthNow = cal.get(2) + 1;
		return iMonthNow;
	}

	public static int getDayNow() {
		Calendar cal = Calendar.getInstance();
		int iDayNow = cal.get(5);
		return iDayNow;
	}

	/**
	 * 秒数换算成hh:mm:ss的形式
	 * @param sec 秒数
	 * @return
	 */
	public static String getTimeFromSecond(String sec) {
		if (null == sec || "".equals(sec.trim())) {
			return "00:00";
		}
		int isec = Integer.parseInt(sec);
		if (isec < 60) {
			return "00:" + formatNum(isec);
		} else {
			int imin = isec / 60;
			int isec2 = isec % 60;
			if (imin < 60) {
				return formatNum(imin) + ":" + formatNum(isec2);
			} else {
				int ihour = imin / 60;
				int imin2 = imin % 60;
				return formatNum(ihour) + ":" + formatNum(imin2) + ":" + formatNum(isec2);
			}
		}
	}

	private static String formatNum(int time) {
		if (time < 10) {
            return "0" + time;
		} else {
			return String.valueOf(time);
		}
	}

	/**
	 * hh:mm:ss换算成秒数的形式
	 * @param 时间
	 * @return
	 */
	public static String getSecondFromTime(String time) {
		if (null == time || "".equals(time.trim())) {
			return "0";
		} else {
			try {
				String[] split = time.split(":");
				int length = split.length;
				int sec = 0;
				if (length == 2) {
					sec = sec + Integer.parseInt(split[0]) * 60;
					sec = sec + Integer.parseInt(split[1]);
				} else if (length == 3) {
					sec = sec + Integer.parseInt(split[0]) * 60 * 60;
					sec = sec + Integer.parseInt(split[1]) * 60;
					sec = sec + Integer.parseInt(split[2]);
				}
				return String.valueOf(sec);
			} catch (Exception e) {
				e.getMessage();
				return "0";
			}
		}
	}

	public static final String getNowDate() {
		return getNowDate(1);
	}

	public static final String getNowDate(int flag) {
		Date d = new Date();
		String nowDate = null;
		nowDate = systemToStrDate(d, flag);
		return nowDate;
	}

	public static final String getNowTime() {
		Date d = new Date();
		String nowDate = null;
		nowDate = systemToStrDate(d, 3);
		return nowDate;

	}

	public static final String getNowDateTime() {
		return getNowDateTime(2);
	}

	public static final String getNowDateTime(int flag) {
		Date d = new Date();
		return systemToStrDate(d, flag);
	}

	/**
	 * 把系统的时间转化为我们需要的时间
	 *
	 * @param date
	 *            系统时间,flag 是标志位 1,表示yyyyMMdd,2表示yyyyMMddHHmmss,3表示HHmmss
	 * @return 我们需要的时间
	 */
	public static final String systemToStrDate(Date date, int flag) {
		if (date == null) {
			return null;
		}
		String tempStr = getDateFormat(flag);
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(tempStr);
		return simpleDateFormat.format(date);
	}

	private static final int[] FRAGMENT = { 4, 2, 2, 2, 2, 2 };
	private static final String[] FRAGMENT_NAME = { "年", "月", "日", "时", "分", "秒" };

	/**
	 * @author zhouyu
	 * Date:Aug 7, 2007
	 * @param time
	 * @param size 年月日时分秒
	 * @return
	 * Return_Type:String
	 * Description:
	  */
	public final static String showTime(String time, int size) {
        StringBuffer sb = new StringBuffer();
		int fragment = 0;
		for (int i = 0; i < size; i++) {
			try {
				fragment = Integer.parseInt(time.substring(0, FRAGMENT[i]));
			} catch (Exception e) {
				fragment = 0;
			}
			sb.append(fragment).append(FRAGMENT_NAME[i]);
			time = time.substring(FRAGMENT[i]);
		}
		return sb.toString();
	}

	/**
	 * 把系统的时间转化为我们需要的时间
	 * @param date 系统时间,flag 是标志位 1,表示yyyyMMdd,2表示yyyyMMddHHmmss,3表示HHmmss
	 * @return 我们需要的时间
	 */
	public static final String dateToStr(Date date, int flag) {
		String tempStr = getDateFormat(flag);
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(tempStr);
		String res = "";
		try {
			res = simpleDateFormat.format(date);
		} catch (Exception e) {
		}
		return res;
	}


	public static String sochi2BeijingStr(String sochiDateStr, int flag) {
		Date date = javax.xml.bind.DatatypeConverter.parseDateTime(sochiDateStr).getTime();
		return dateToStr(date, flag);
	}

	/**
	 *
	 * @param starttime
	 * @param step sec
	 * @return
	 */
	public static String getTimeByStep(String starttime, int step) {
		Date time = strDateToSystem(starttime, 2);
		Calendar cal = Calendar.getInstance();
		cal.setTime(time);
		cal.add(Calendar.SECOND, step);

		return dateToStr(cal.getTime(), 2);
	}

	public static long getTimeByString(String time) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
		try {
			return dateFormat.parse(time).getTime();
		} catch (Exception e) {
			return 0L;
		}
	}

	public static String getDateTimeByLong(long l) {
		Date t = new Date(l);
		return "" + t.getYear() + "-" + t.getMonth() + "-" + t.getDay();
	}

	public static Date getDateByMillis(long millis) {
		Date date = new Date(millis);
		return date;
	}

	public static Date getMondayOfThisWeek() {
		Calendar c = Calendar.getInstance();
		int dayofweek = c.get(Calendar.DAY_OF_WEEK) - 1;
		if (dayofweek == 0)
			dayofweek = 7;
		c.add(Calendar.DATE, -dayofweek + 1);
		return c.getTime();
	}

	public static Date getSundayOfThisWeek() {
		Calendar c = Calendar.getInstance();
		int dayofweek = c.get(Calendar.DAY_OF_WEEK) - 1;
		if (dayofweek == 0)
			dayofweek = 7;
		c.add(Calendar.DATE, -dayofweek + 7);
		return c.getTime();
	}

	public static String getFirstDayOfPreviousMonth() {
		return getFirstDayOfPreviousMonth(6);
	}

	public static String getLastDayOfPreviousMonth() {
		return getLastDayOfPreviousMonth(6);
	}

	/**
	 * 获取上月第一天
	 * @param flag
	 * @return
	 */
	public static String getFirstDayOfPreviousMonth(int flag) {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, -1);
		c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为上月第一天
		String tempStr = getDateFormat(flag);
		SimpleDateFormat format = new SimpleDateFormat(tempStr);
		String firstDay = format.format(c.getTime());
		return firstDay;
	}

	public static String getLastDayOfPreviousMonth(int flag) {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, -1);
		c.set(Calendar.DAY_OF_MONTH, 0);
		String tempStr = getDateFormat(flag);
		SimpleDateFormat format = new SimpleDateFormat(tempStr);
		String firstDay = format.format(c.getTime());
		return firstDay;
	}

	/**
	 * @return 获取下月最后一天
	 */
	public static String getLastDayOfNextMonth() {
		return getLastDayOfNextMonth(6);
	}

	public static String getLastDayOfNextMonth(int flag) {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, 1);
		c.set(Calendar.DATE, 1);
		c.roll(Calendar.DATE, -1);//日期回滚一天，也就是本月最后一天 
		String tempStr = getDateFormat(flag);
		SimpleDateFormat format = new SimpleDateFormat(tempStr);
		String firstDay = format.format(c.getTime());
		return firstDay;
	}

	public static String getFirstDayOfMonth() {
		return getFirstDayOfMonth(6);
	}

	public static String getFirstDayOfMonth(int flag) {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, 0);
		c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
		String tempStr = getDateFormat(flag);
		SimpleDateFormat format = new SimpleDateFormat(tempStr);
		String firstDay = format.format(c.getTime());
		return firstDay;
	}

	public static String getFirstDayOfMonth(int year, int month) {
		return getFirstDayOfMonth(year, month, 6);
	}

	public static String getFirstDayOfMonth(int year, int month, int flag) {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH, month - 1);
		c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
		String tempStr = getDateFormat(flag);
		SimpleDateFormat format = new SimpleDateFormat(tempStr);
		String firstDay = format.format(c.getTime());
		return firstDay;
	}

	public static String getFirstDayOfYear(int year) {
		return getFirstDayOfYear(year, 6);
	}

	public static String getFirstDayOfYear(int year, int flag) {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH, 0);
		c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
		String tempStr = getDateFormat(flag);
		SimpleDateFormat format = new SimpleDateFormat(tempStr);
		String firstDay = format.format(c.getTime());
		return firstDay;
	}

	public static String getFirstDayOfWeek() {
		return getFirstDayOfWeek(6);
	}

	/**
	 * 获取指定年、月，周的第一天
	 * @param year
	 * @param month
	 * @param week
	 * @return
	 */
	public static String getFirstDayOfWeek(int year, int month, int week) {
		return getFirstDayOfWeek(year, month, week, 6);
	}

	public static String getFirstDayOfWeek(int year, int month, int week, int flag) {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH, month - 1);
		c.set(Calendar.DATE, 1);

		Calendar cal = (Calendar) c.clone();
		cal.add(Calendar.DATE, (week - 1) * 7);

		Calendar calendar = Calendar.getInstance();
		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		calendar.setTime(cal.getTime());
		calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());

		String tempStr = getDateFormat(flag);
		SimpleDateFormat format = new SimpleDateFormat(tempStr);
		String firstDay = format.format(calendar.getTime());
		return firstDay;
	}

	public static String getFirstDayOfWeek(int flag) {
		Calendar c = Calendar.getInstance();
		int w = c.get(Calendar.DAY_OF_WEEK) - 1;
		Date curDay = null;
		if (w == 0)//周日{
		{
			int day = getDaysOfNow2SundayInWeek() + 1; // 加1，即周一离本周日的间隔天数
			GregorianCalendar gCalendar = new GregorianCalendar();

			gCalendar.add(GregorianCalendar.DATE, day - 7); // 计算与本周一相差的时间间隔
			curDay = gCalendar.getTime();
		} else {
			int day = getDaysOfNow2SundayInWeek() + 1; // 加1，即周一离本周日的间隔天数
			GregorianCalendar gCalendar = new GregorianCalendar();

			gCalendar.add(GregorianCalendar.DATE, day); // 计算与本周一相差的时间间隔
			curDay = gCalendar.getTime();
		}
		String tempStr = getDateFormat(flag);
		SimpleDateFormat format = new SimpleDateFormat(tempStr);
		String firstDay = format.format(curDay.getTime());
		return firstDay;
	}

	// 获取当前日期与本周日的相差天数
	public static int getDaysOfNow2SundayInWeek() {
		Calendar calendar = Calendar.getInstance();

		int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1; // 减一天，周一为1，符合中国人习惯。Sunday - 1; Monday - 2; Saturday - 7
		if (dayOfWeek == 0) { // 周日
			return 0;
		} else {
			return 0 - dayOfWeek;
		}
	}

	public static String getFirstDayOfNextWeek() {
		return getFirstDayOfNextWeek(6);
	}

	public static String getFirstDayOfNextWeek(int flag) {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		c.add(Calendar.WEEK_OF_YEAR, 1);
		String tempStr = getDateFormat(flag);
		SimpleDateFormat format = new SimpleDateFormat(tempStr);
		String firstDay = format.format(c.getTime());
		return firstDay;
	}

	public static String getLastDayOfWeek() {
		return getLastDayOfWeek(6);
	}

	public static String getLastDayOfWeek(int year, int month, int week) {
		return getLastDayOfWeek(year, month, week, 6);
	}

	public static String getLastDayOfWeek(int year, int month, int week, int flag) {
		String firstDay = getFirstDayOfWeek(year, month, week);
		Date date = strToDate(firstDay, 6);
		Date endDate = addDate(date, 6);
		return dateToStr(endDate, 6);

	}

	public static String getLastDayOfWeek(int flag) {
		Calendar c = Calendar.getInstance();
		int w = c.get(Calendar.DAY_OF_WEEK) - 1;
		if (w == 0)//周日{
		{
			return getNowDate(flag);
		} else {
			c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
			c.add(Calendar.WEEK_OF_YEAR, 1);
			String tempStr = getDateFormat(flag);
			SimpleDateFormat format = new SimpleDateFormat(tempStr);
			String firstDay = format.format(c.getTime());
			return firstDay;
		}

	}

	public static String getLastDayOfNextWeek() {
		return getLastDayOfNextWeek(6);
	}

	public static String getLastDayOfNextWeek(int flag) {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		c.add(Calendar.WEEK_OF_YEAR, 2);
		String tempStr = getDateFormat(flag);
		SimpleDateFormat format = new SimpleDateFormat(tempStr);
		String firstDay = format.format(c.getTime());
		return firstDay;
	}

	public static String getLastDayOfMonth() {
		return getLastDayOfMonth(6);
	}

	public static String getLastDayOfMonth(int flag) {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
		String tempStr = getDateFormat(flag);
		SimpleDateFormat format = new SimpleDateFormat(tempStr);
		String lastDay = format.format(c.getTime());
		return lastDay;
	}

	public static String getLastDayOfMonth(int year, int month) {
		return getLastDayOfMonth(year, month, 6);
	}

	public static String getLastDayOfMonth(int year, int month, int flag) {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH, month - 1);
		c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
		String tempStr = getDateFormat(flag);
		SimpleDateFormat format = new SimpleDateFormat(tempStr);
		String lastDay = format.format(c.getTime());
		return lastDay;
	}

	public static String getLastDayOfYear(int year) {
		return getLastDayOfYear(year, 6);
	}

	public static String getLastDayOfYear(int year, int flag) {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH, 11);
		c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
		String tempStr = getDateFormat(flag);
		SimpleDateFormat format = new SimpleDateFormat(tempStr);
		String lastDay = format.format(c.getTime());
		return lastDay;
	}

	/** 
	 * 根据年 月 获取对应的月份 天数 
	 * */
	public static int getDaysByYearMonth(int year, int month) {

		Calendar a = Calendar.getInstance();
		a.set(Calendar.YEAR, year);
		a.set(Calendar.MONTH, month - 1);
		a.set(Calendar.DATE, 1);
		a.roll(Calendar.DATE, -1);
		int maxDate = a.get(Calendar.DATE);
		return maxDate;
	}

	public static String getMonth(String curDate, int month) {
		Date date = strDateToSystem(curDate + "01", 1);
		Calendar a = Calendar.getInstance();
		a.setTime(date);
		a.add(Calendar.MONTH, month);
		String tempStr = getDateFormat(20);
		SimpleDateFormat format = new SimpleDateFormat(tempStr);
		return format.format(a.getTime());
	}

	/**
	 * 获取过去的分钟
	 * @param date
	 * @return
	 */
	public static long pastMinutes(Date date) {
		long t = new Date().getTime() - date.getTime();
		return t / (60 * 1000);
	}

	/**
	 *  获取过去的小时
	 * @param date
	 * @return
	 */
	public static long pastHours(Date date) {
		long t = new Date().getTime() - date.getTime();
		return t / (60 * 60 * 1000);
	}

	/**
	 * 日期型字符串转化为日期 格式
	 * { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", 
	 *   "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm",
	 *   "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm" }
	 */
	public static Date parseDate(Object str) {
		if (str == null) {
			return null;
		}
		try {
			return parseDate(str.toString(), parsePatterns);
		} catch (ParseException e) {
			return null;
		}
	}

	/** 
	* 两个时间相差距离多少天多少小时多少分多少秒 
	* @param str1 时间参数 1 格式：1990-01-01 12:00:00 
	* @param str2 时间参数 2 格式：2009-01-01 12:00:00 
	* @return String 返回值为：xx天xx小时xx分xx秒 
	*/
	public static String getDistanceTime(String str1, String str2) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date one;
		Date two;
		long day = 0;
		long hour = 0;
		long min = 0;
		long sec = 0;
		try {
			one = df.parse(str1);
			two = df.parse(str2);
			long time1 = one.getTime();
			long time2 = two.getTime();
			long diff;
			if (time1 < time2) {
				diff = time2 - time1;
			} else {
				diff = time1 - time2;
			}
			day = diff / (24 * 60 * 60 * 1000);
			hour = (diff / (60 * 60 * 1000) - day * 24);
			min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
			sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return day + "天" + hour + "小时" + min + "分" + sec + "秒";
	}

	/** 
	 * 系统时间距离给定时间相差距离多少天多少小时多少分多少秒
	 * @param str2 时间参数 2 格式：2009-01-01 12:00:00 
	 * @return String 返回值为：xx天xx小时xx分xx秒 
	 */
	public static String getNowDistanceTime(String str) {
		if (StringUtils.isBlank(str)) {
			return "";
		}
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date one;
		Date two;
		long day = 0;
		long hour = 0;
		long min = 0;
		long sec = 0;
		String str1 = getDateTime();
		String distance = "";
		try {
			one = df.parse(str1);
			two = df.parse(str);
			long time1 = one.getTime();
			long time2 = two.getTime();
			long diff;
			if (time1 < time2) {
				diff = time2 - time1;
			} else {
				diff = time1 - time2;
			}
			day = diff / (24 * 60 * 60 * 1000);
			hour = (diff / (60 * 60 * 1000) - day * 24);
			min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
			sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
			if (time1 < time2) {
				distance = "还剩:" + day + "天" + hour + "小时" + min + "分" + sec + "秒";
			} else {
				distance = "过期:" + day + "天" + hour + "小时" + min + "分" + sec + "秒";
			}
		} catch (ParseException e) {
			// e.printStackTrace();  
		}
		return distance;

	}

	/**
	 * 时间比较
	 * @param time1
	 * @param time2
	 * @return
	 * @throws ParseException
	 */
	public static boolean compare(String time1, String time2) throws ParseException {
		return compare("yyyy-MM-dd hh:mm:ss", time1, time2);
	}

	public static boolean compareDate(String time1, String time2) throws ParseException {
		return compare("yyyy-MM-dd", time1, time2);
	}

	private static boolean compare(String pattern, String time1, String time2) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		//将字符串形式的时间转化为Date类型的时间
		Date a = sdf.parse(time1);
		Date b = sdf.parse(time2);
		//Date类的一个方法，如果a早于b返回true，否则返回false
        return a.before(b);
	}

	public static boolean isEffectiveDate(String nowTime, String startTime, String endTime){
		Date nowTimeD = strToDate(nowTime,8);
		Date startTimeD= strToDate(nowTime,8);
		Date endTimeD= strToDate(nowTime,8);
		return isEffectiveDate(nowTimeD,startTimeD,endTimeD);
	}

	/**
	 *
	 * @param nowTime
	 * @param startTime
	 * @param endTime
	 * @return 判断某个时间是否在某个时间区域
	 */
	public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
		if (nowTime.getTime() == startTime.getTime()
				|| nowTime.getTime() == endTime.getTime()) {
			return true;
		}

		Calendar date = Calendar.getInstance();
		date.setTime(nowTime);

		Calendar begin = Calendar.getInstance();
		begin.setTime(startTime);

		Calendar end = Calendar.getInstance();
		end.setTime(endTime);

        return date.after(begin) && date.before(end);
	}

	public static final String getNowDateTime8() {
		Date d = new Date();
		return systemToStrDate(d, 8);
	}

    public static long diffDays(Date startTime, Date endTime) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(startTime);
        cal.set(Calendar.HOUR, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        long s = cal.getTimeInMillis();

        cal.setTime(endTime);
        cal.add(Calendar.DAY_OF_YEAR, 1);
        cal.set(Calendar.HOUR, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        long e = cal.getTimeInMillis();

        return TimeUnit.DAYS.convert(e - s, TimeUnit.MILLISECONDS);
    }

	public static boolean isDate(String dttm) {
		return isDate(dttm, "yyyy-MM-dd") || (isDate(dttm, "yyyy/MM/dd"));

	}
	public static boolean isDate(String dttm, String format) {
        if (dttm == null || dttm.isEmpty() || format == null || format.isEmpty()) {
            return false;
        }

        if (format.replaceAll("'.+?'", "").indexOf("y") < 0) {
            format += "/yyyy";
            DateFormat formatter = new SimpleDateFormat("/yyyy");
            dttm += formatter.format(new Date());
        }

        DateFormat formatter = new SimpleDateFormat(format);
        formatter.setLenient(false);
        ParsePosition pos = new ParsePosition(0);
        Date date = formatter.parse(dttm, pos);

        if (date == null || pos.getErrorIndex() > 0) {
            return false;
        }
        if (pos.getIndex() != dttm.length()) {
            return false;
        }

        return formatter.getCalendar().get(Calendar.YEAR) >= 1000 && formatter.getCalendar().get(Calendar.YEAR) <= 9999;
    }

	public static void main(String[] argc) {
		/*Map<String,String> datMap = new HashMap<String,String>();
		//上周
		for( int i=7;i>0 ;i-- ){
			String date =  DateUtil.dateToStr(  DateUtil.addDate( DateUtil.getMondayOfThisWeek(), -i) , 6);
			datMap.put(date, "0");
			System.out.println(date);
		}
		
		//本周及下一周
		
		for( int i=0;i<14;i++ )
		{
			String date =  DateUtil.dateToStr(  DateUtil.addDate( DateUtil.getMondayOfThisWeek(), i) , 6);
			datMap.put(date, "0");
			System.out.println(date);
		}*/

		/*System.out.println(System.currentTimeMillis());
		System.out.println(DateUtil.formatY_M_D(1242664425312L));
		System.out.println(DateUtil.formatStrTime("20081012203030", 2, 8));
		String nowTime = DateUtil.formatStrTime(getNowDateTime(), 2, 6) ;
		String befortTime = DateUtil.formatStrTime("20110425180200", 2, 6) ;
		System.out.println( DateUtil.daySubtract(nowTime, befortTime) );
		*/

		/*String day = "6月13日";
		String date = "2014年3月26日";
		day = DateUtil.getNowDate();
		day  =  DateUtil.formatStrTime(day,1,14);
		day = "2014年3月26日 04:00:00";
		System.out.println( DateUtil.formatChinaStrTime(date,16,6) );
		day="2014-04-22";
		System.out.println( DateUtil.formatStrTime(day,6,15) );
		System.out.println( DateUtil.getFirstDayOfPreviousMonth() );
		System.out.println( DateUtil.getLastDayOfNextMonth() );*/
		/*System.out.println( DateUtil.daySubtract("2016-02-25 13:48:33")+"||2016-02-25" );
		System.out.println( DateUtil.daySubtract("2016-02-26 13:48:33")+"||2016-02-26" );
		System.out.println( DateUtil.daySubtract("2016-02-27 13:48:33")+"||2016-02-27" );*/
		/*System.out.println( DateUtil.getFirstDayOfWeek() );
		System.out.println( DateUtil.getLastDayOfWeek() );
		
		System.out.println( DateUtil.getFirstDayOfNextWeek() );
		System.out.println( DateUtil.getLastDayOfNextWeek() );
		
		System.out.println( DateUtil.getMonth("201604", 2));*/
		//    	System.out.println( DateUtil.getFirstDayOfWeek(2016, 11, 1));
		//    	System.out.println( DateUtil.getLastDayOfWeek(2016, 11, 1));
		//    	System.out.println( DateUtil.getFirstDayOfMonth(2017, 2));
		//    	System.out.println( DateUtil.getLastDayOfMonth(2017, 2));
		//    	System.out.println( DateUtil.getFirstDayOfYear(2017));
		//    	System.out.println( DateUtil.getLastDayOfYear(2017));
		//  	System.out.println(DateUtil.strAmPmDateToSystem("2016/01/02_pm_11"));
		/*    	
		    	Date d1 = DateUtil.strToDate("2018-01-11", 6);
		    	Date d2 = DateUtil.strToDate("2018-04-20", 6);
		//    	d2 = DateUtil.strToDate("2018-01-11", 6);
		    	System.out.println(diffDays(d1, d2));*/

		boolean result = false;
        //			result = DateUtil.compare("2012-11-30 16:18:18", getNowDateTime8());
        String dttm = "19-02-27";
		result = DateUtil.isDate(dttm);
//		Date a = cn.hutool.core.date.DateUtil.parseDate(dttm);
//		cn.hutool.core.date.DateUtil.formatDate(a);
//
//		System.out.println(cn.hutool.core.date.DateUtil.formatDate(a));
		System.out.println(result);
	}




}
